/**
 * Created by YangBin on 2016/10/31.
 */
//供货信息搜索接口  2.1.4  转跳页面
var urlType = "/home/searchProvideInfos";

//请求数据
var urlAddress = window.location.href;

var urlArr = [];
var dataType = '';
var meatType = '';
var $window = $(window),
    viewHeight = $window.height(),
    contentHeight = $('#meal-info ul').height(),
    loadNum = 0,
    loadTotal = 0;
loadTotalSurplus = 10,
    time = '',
    keyWord = '',
    country = '',
    offset = 0;

keyWord = getQueryString('keyWord');
country =  getQueryString('country');
time = getQueryString('updateDate');
meatType = getQueryString('meatType');

if(keyWord == null){
    keyWord = '';
}
if(country == null){
    country = '';
}
if(time == null){
    time = 30;
}
//  console.log(contentHeight,viewHeight);
// if(urlAddress.indexOf("?")!= -1){
//
// 	var urlArr = urlAddress.split('?');
//
// 	dataType = urlArr[1];
//
// 	var urlArr1 = dataType.split('=');
// 	console.log(urlArr1)
// 	urlArr1 =urlArr1[1].charAt(0)
// }

if(meatType == 0){
    $('#side_delivery_nav li').eq(1).text('牛肉')

}else{
    $('#side_delivery_nav li').eq(1).text('羊肉')

}

$('#side_delivery_nav li').on('click',function(){

    if($(this).index() == 0){
        //返回首页
        window.location.href = 'index.html';

    }else if($(this).index()== 2){
        //再次请求ajax  刷新页面
        // askGetData2(urlType1);
    }
})

dataType = "meatType=" + meatType + "&pageSize=" + 10 + "&offset=" + 0 + "&updateDate=" +time +"&keyWord=" + keyWord + "&country=" + country;
askGetData2(urlType,dataType);
function askGetData2(num,dataType){
    $.ajax({
        url:host + num,
        type:'post',
        data:dataType,
        success:function(res){
            //这里是分的页数
            console.log(res.data)
            loadTotal = Math.ceil(res.total/10);
            loadTotalSurplus = res.total%10;
            if(loadNum == loadTotal-1){
                loadTotalSurplus = loadNum
            }
            if(res.success == true){
                if(res.data.length <=0){

                }else{
                    createLi(res.data)
                    clickLi(res.data)
                    contentHeight = $('#meal-info ul').height();
                }
            }else{
                //就是无数据则返回什么
            }
        }
    });
}
//请求后页面数据交互
function createLi(num){
    // console.log(num)
//
    for (var i = 0; i < num.length; i++) {

        if(num[i].imageList.length>=1){
            $('#meal-info ul').append('<li><div class="row"><div class="col-xs-3 height-equals-width"><img src=" '+ num[i]['imageList'][0] +' "></div><div class="col-xs-9"><p>'+ num[i]['meatCategory'] +'</p><p><span>产地:<em>&nbsp'+ num[i]['country'] +'</em></span><span>货源地：<em>'+ num[i]['warehouse'] +'</em></span></p><p><span>数量<em>&nbsp'+ Math.round(num[i]['amount']/1000*100)/100 +'吨</em></span><span>包装规格: <em>'+ num[i]['packing'] +'</em></p></div></div></li>');
            // $('#meal-info ul').append('<li><div class="row"><div class="col-xs-3 height-equals-width"><img src=" '+ num[i]['imageList'][0] +' "></div><div class="col-xs-9"><p>'+ num[i]['meatCategory'] +'</p><p><span>产地:<em>&nbsp'+ num[i]['country'] +'</em></span><span>货源地：<em>'+ num[i]['warehouse'] +'</em></span></p><p><span>数量<em>&nbsp'+ num[i]['amount'] +'kg</em></span><span>包装规格: <em>'+ num[i]['packing'] +'</em></p></div></div></li>');

        }else{
            $('#meal-info ul').append('<li><div class="row"><div class="col-xs-3 height-equals-width"><img src="img/defaultImg.png"></div><div class="col-xs-9"><p>'+ num[i]['meatCategory'] +'</p><p><span>产地:<em>&nbsp'+ num[i]['country'] +'</em></span><span>货源地：<em>'+ num[i]['warehouse'] +'</em></span></p><p><span>数量<em>&nbsp'+ Math.round(num[i]['amount']/1000*100)/100 +'吨</em></span><span>包装规格: <em>'+ num[i]['packing'] +'</em></p></div></div></li>');
            // $('#meal-info ul').append('<li><div class="row"><div class="col-xs-3 height-equals-width"><img src="img/defaultImg.png"></div><div class="col-xs-9"><p>'+ num[i]['meatCategory'] +'</p><p><span>产地:<em>&nbsp'+ num[i]['country'] +'</em></span><span>货源地：<em>'+ num[i]['warehouse'] +'</em></span></p><p><span>数量<em>&nbsp'+ num[i]['amount'] +'kg</em></span><span>包装规格: <em>'+ num[i]['packing'] +'</em></p></div></div></li>');
        }
    }
}
function clickLi(num1){
    $('#meal-info li').on('click',function(){
//			console.log()
        liIndex = $(this).index()

        //可以挖坑
        Cookie.setCookie('detailed',num1[liIndex%10]['id']);
        //这里可以直接转跳
        window.location.href ="deliveryDetails.html";
    })
}

//	//滑动加载更多
var $window = $(window),
    viewHeight = $window.height(),
    contentHeight = $('#meal-info ul').height();

$window.on('scroll', function() {
    var scrollTop = $(this).scrollTop();
    console.log(contentHeight,viewHeight,scrollTop)
    if (scrollTop + viewHeight >= contentHeight +10){
        loadNum++;
        contentHeight = contentHeight*(loadNum+1);

        dataType = "meatType=" + meatType + "&pageSize=" + loadTotalSurplus + "&offset=" + loadNum + "&updateDate=" +time +"&keyWord=" + keyWord + "&country=" + country;

        console.log(loadNum,loadTotal)
        if(loadNum >= loadTotal){

            $('#meal-info ul').append('<div id="meal-infoLi">暂无更多数据.....</div>');
            return;

        }else{
            if(loadNum == (loadTotal-1)){

                dataType = "meatType=" + meatType + "&pageSize=" + loadTotalSurplus + "&offset=" + loadNum + "&updateDate=" +time +"&keyWord=" + keyWord + "&country=" + country;

                askGetData2(urlType,dataType);

            }else{

            }
        }
    }
})